package com.ldn.server.mqtt.listener;

import com.ldn.server.mqtt.covert.MqttCovert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.codec.MqttPublishMessage;
import org.dromara.mica.mqtt.codec.MqttQoS;
import org.dromara.mica.mqtt.core.server.event.IMqttMessageListener;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.tio.core.ChannelContext;

/**
 * MQTT服务端消息监听器
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @product serverless
 * @className ServerMessageListener
 * @createDate 2025/2/8 - 16:30
 * @description MQTT服务端消息监听器
 **/
@Slf4j
@Service
public class ServerMessageListener implements IMqttMessageListener, SmartInitializingSingleton {

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public void onMessage(ChannelContext context, String clientId, String topic, MqttQoS qoS, MqttPublishMessage message) {
        MqttCovert covert = applicationContext.getBean(MqttCovert.class);
        covert.messageHandler(context, clientId, topic, qoS, message);
    }

    @Override
    public void afterSingletonsInstantiated() {
        log.info("afterSingletonsInstantiated: 初始化");
    }
}
